﻿using Station.Inspect;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using thinger.VisionModels;
using thinger.VisitionDAL;

namespace thinger.VisionDemo
{
    public enum GoNames
    {
        检测A1,
        检测B1,
        打码A1,
        打码B1,
        读码A1,
        读码B1,
        检测A2,
        检测B2,
        打码A2,
        打码B2,
        读码A2,
        读码B2,
    }
    public partial class FrmOnes : Form
    {
        private SysConfig sysConfig;
        private H5U h5u;
        Thread inspectThread;   //运动线程
        private GoNames goNames;
        public FrmOnes(H5U h5u, SysConfig sysConfig)
        {
            this.sysConfig = sysConfig;
            this.h5u = h5u;
            InitializeComponent();
        }

        private static string CurrentTime
        {
            get { return DateTime.Now.ToString("HH:mm:ss"); }
        }
        public void AddLog(int index, string log)
        {
            try
            {
                if (!this.lst_RunLog.InvokeRequired)
                {
                    ListViewItem lst = new ListViewItem("   " + CurrentTime, index);
                    lst.SubItems.Add(log);
                    this.lst_RunLog.Items.Add(lst);
                    this.lst_RunLog.Items[this.lst_RunLog.Items.Count - 1].EnsureVisible();
                }
                else
                {
                    this.lst_RunLog.Invoke(new Action<int, string>(AddLog), index, log);
                }
                if (index == 0)
                {
                    //demoTCP.log_add(log, "运行正常");
                    LogRecord.WriteOperLog(log + "  半自动运行正常");

                }
                else
                {
                    //demoTCP.log_add(log, "运行异常");
                    LogRecord.WriteOperLog(log + "  半自动运行异常");
                }
            }
            catch (Exception)
            {

            }

        }


        private bool isAuto()
        {
            var result = h5u.ReadH5U(4090);
            if (result.Message == "11")
            {
                return true;
            }
            return false;
        }
        private void btn_loadPath_Click(object sender, EventArgs e)
        {
            StaInspect.Instance.Load(sysConfig.CheckModePath);//加载模板
        }

        private void btn_fuPanA1_Click(object sender, EventArgs e)
        {
            goNames = GoNames.检测A1;
            goRunSlection();
        }

        private void btn_fuPanB1_Click(object sender, EventArgs e)
        {
            goNames = GoNames.检测B1;
            goRunSlection();
        }

        private void btn_daMaA1_Click(object sender, EventArgs e)
        {
            goNames = GoNames.打码A1;
            goRunSlection();
        }

        private void btn_daMaB1_Click(object sender, EventArgs e)
        {
            goNames = GoNames.打码B1;
            goRunSlection();
        }

        private void btn_duMaA1_Click(object sender, EventArgs e)
        {
            goNames = GoNames.读码A1;
            GlobalVar.Instance.isReadOn = false;
            GlobalVar.Instance.dumaCoder = "";
            GlobalVar.Instance.dumaStr = "搬运1-读码A";
            goRunSlection();
        }

        private void btn_duMaB1_Click(object sender, EventArgs e)
        {
            goNames = GoNames.读码B1;
            GlobalVar.Instance.isReadOn = false;
            GlobalVar.Instance.dumaCoder = "";
            GlobalVar.Instance.dumaStr = "搬运1-读码B";
            goRunSlection();
        }

        private void btn_fuPanA2_Click(object sender, EventArgs e)
        {
            goNames = GoNames.检测A2;
            goRunSlection();
        }

        private void btn_fuPanB2_Click(object sender, EventArgs e)
        {
            goNames = GoNames.检测B2;
            goRunSlection();
        }

        private void btn_daMaA2_Click(object sender, EventArgs e)
        {
            goNames = GoNames.打码A2;
            goRunSlection();
        }

        private void btn_daMaB2_Click(object sender, EventArgs e)
        {
            goNames = GoNames.打码B2;
            goRunSlection();
        }

        private void btn_duMaA2_Click(object sender, EventArgs e)
        {

            goNames = GoNames.读码A2;
            GlobalVar.Instance.isReadOn = false;
            GlobalVar.Instance.dumaCoder = "";
            GlobalVar.Instance.dumaStr = "搬运2-读码A";
            goRunSlection();
        }

        private void btn_duMaB2_Click(object sender, EventArgs e)
        {
            goNames = GoNames.读码B2;
            GlobalVar.Instance.isReadOn = false;
            GlobalVar.Instance.dumaCoder = "";
            GlobalVar.Instance.dumaStr = "搬运2-读码B";
            goRunSlection();
        }

        private void goRunSlection()
        {
            if (string.IsNullOrEmpty(StaInspect.Instance.loadPath))
            {
                MessageBox.Show("没有加载程式！无法启动程序。");
                return;
            }
            if (isAuto())
            {
                MessageBox.Show("当前处在自动运行模式！无法启动程序。");
                return;
            }
            if (goNames.ToString().IndexOf("打码") > -1)
            {
                Mainpro.Instance.PrintConnect();
                // MessageBox.Show("打码");
                if (Mainpro.Instance.IsReadH5u(4204, "11"))
                {

                    GlobalVar.Instance.homMat2D_dama_a1 = Mainpro.Instance.GetHomMat2D(sysConfig.GetCoderModelA1, "搬运1-A面");
                    if (GlobalVar.Instance.homMat2D_dama_a1 == null)
                    {
                        MessageBox.Show("请设置搬运1-A面打码标定模板再启动！");
                        return;
                    }


                    GlobalVar.Instance.homMat2D_dama_a2 = Mainpro.Instance.GetHomMat2D(sysConfig.GetCoderModelA2, "搬运2-A面");
                    if (GlobalVar.Instance.homMat2D_dama_a2 == null)
                    {
                        MessageBox.Show("请设置搬运2-A面打码标定模板再启动！");
                        return;
                    }
                }

                if (Mainpro.Instance.IsReadH5u(4206, "11"))
                {
                    GlobalVar.Instance.homMat2D_dama_b1 = Mainpro.Instance.GetHomMat2D(sysConfig.GetCoderModelB1, "搬运1-B面");
                    if (GlobalVar.Instance.homMat2D_dama_b1 == null)
                    {
                        MessageBox.Show("请设置搬运1-B面打码标定模板再启动！");
                        return;
                    }
                    GlobalVar.Instance.homMat2D_dama_b2 = Mainpro.Instance.GetHomMat2D(sysConfig.GetCoderModelB2, "搬运2-B面");
                    if (GlobalVar.Instance.homMat2D_dama_b2 == null)
                    {
                        MessageBox.Show("请设置搬运2-B面打码标定模板再启动！");
                        return;
                    }
                }

            }
            if (inspectThread != null)
            {
                inspectThread.Abort();
            }
            inspectThread = new Thread(new ThreadStart(slectionRun));
            inspectThread.IsBackground = true;
            inspectThread.Start();
        }
        private void slectionRun()
        {
            Mainpro.Instance.IsAutoRun = false;
            switch (goNames)
            {
                case GoNames.检测A1:
                    GlobalVar.Instance.OrderData1 = new OrderData();
                    GlobalVar.Instance.getFinished = true;
                    GlobalVar.Instance.ThreadControl = true;
                    Mainpro.Instance.Check_Run_A(1);
                    break;

                case GoNames.检测B1:
                    GlobalVar.Instance.OrderData1 = new OrderData();
                    GlobalVar.Instance.ThreadControl = true;
                    Mainpro.Instance.Check_Run_B(1);
                    break;

                case GoNames.打码A1:
                    Mainpro.Instance.plc_dama("A", 1, GlobalVar.Instance.OrderData1, GlobalVar.Instance.cellInfos_1, GlobalVar.Instance.comInfos_1, GlobalVar.Instance.homMat2D_dama_a1, GlobalVar.Instance.homMat2D_dama_b1, 4030);

                    break;

                case GoNames.打码B1:
                    Mainpro.Instance.plc_dama("B", 1, GlobalVar.Instance.OrderData1, GlobalVar.Instance.cellInfos_1, GlobalVar.Instance.comInfos_1, GlobalVar.Instance.homMat2D_dama_a1, GlobalVar.Instance.homMat2D_dama_b1, 4032);

                    break;

                case GoNames.读码A1:
                    GlobalVar.Instance.isReadOn = true;
                    break;

                case GoNames.读码B1:
                    GlobalVar.Instance.isReadOn = true;
                    break;

                case GoNames.检测A2:
                    GlobalVar.Instance.OrderData2 = new OrderData();
                    GlobalVar.Instance.ThreadControl = true;
                    GlobalVar.Instance.getFinished = true;
                    Mainpro.Instance.Check_Run_A(2);
                    break;

                case GoNames.检测B2:
                    GlobalVar.Instance.OrderData2 = new OrderData();
                    GlobalVar.Instance.ThreadControl = true;
                    Mainpro.Instance.Check_Run_B(2);
                    break;

                case GoNames.打码A2:
                    Mainpro.Instance.plc_dama("A", 2, GlobalVar.Instance.OrderData2, GlobalVar.Instance.cellInfos_2, GlobalVar.Instance.comInfos_2, GlobalVar.Instance.homMat2D_dama_a2, GlobalVar.Instance.homMat2D_dama_b2, 4070);

                    break;

                case GoNames.打码B2:
                    Mainpro.Instance.plc_dama("B", 2, GlobalVar.Instance.OrderData2, GlobalVar.Instance.cellInfos_2, GlobalVar.Instance.comInfos_2, GlobalVar.Instance.homMat2D_dama_a2, GlobalVar.Instance.homMat2D_dama_b2, 4072);

                    break;

                case GoNames.读码A2:

                    GlobalVar.Instance.isReadOn = true;
                    Mainpro.Instance.plc_duma("A", 2, GlobalVar.Instance.OrderData2, 4080, 4084);
                    break;

                case GoNames.读码B2:
                    GlobalVar.Instance.isReadOn = true;
                    break;

            }
            if (goNames.ToString().IndexOf("读码") > -1)
            {
                while (string.IsNullOrEmpty(GlobalVar.Instance.dumaCoder))
                {
                    Thread.Sleep(10);
                }
                GlobalVar.Instance.isReadOn = false;
            }
            inspectThread.Abort();
        }


        private void FrmOnes_Load(object sender, EventArgs e)
        {
            this.lst_RunLog.Columns[1].Width = this.lst_RunLog.Width - this.lst_RunLog.Columns[0].Width - 30;
            Mainpro.Instance.OnAddLogRecive += OnAddLogRecive;
            Mainpro.Instance.OnOneAddLogRecive += OnAddLogRecive;
        }

        private void OnAddLogRecive(object sender, string e)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new Action<object, string>(OnAddLogRecive), sender, e);
            }
            else
            {
                if (e.IndexOf(",") > -1)
                {
                    var pos = e.Split(',');
                    if (pos[0] == "0")
                    {
                        AddLog(0, pos[1]);
                    }
                    else
                    {
                        AddLog(1, pos[1]);
                    }

                }
                else
                {
                    AddLog(0, e);
                }
            }
        }

        private void btn_run1_Click(object sender, EventArgs e)
        {
            if (!isAuto())
            {
                MessageBox.Show("当前处在手动运行模式！无法启动程序。");
                return;
            }
            if (inspectThread != null)
            {
                inspectThread.Abort();
            }
            inspectThread = new Thread(new ThreadStart(goRunAuto));
            inspectThread.IsBackground = true;
            inspectThread.Start();

        }
        private void goRunAuto()
        {

        }






    }
}
